package com.cskaoyan.controller.promotion;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.promotion.coupon.*;
import com.cskaoyan.service.promotion.CouponService;
import com.cskaoyan.util.DateUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: WD created on 2021-09-16 16:56
 * @Version:
 */

@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;


    @RequestMapping("list")
    public BaseRespVo getCategoryList(BaseParam param, String name, Short type, Short status) {
        ShowCouponListVO list = couponService.getCouponList(param, name, type, status);
        return BaseRespVo.ok(list, "成功");
    }

    @RequiresPermissions(value ={"*","admin:coupon:read"},logical = Logical.OR)
    @RequestMapping("read")
    public BaseRespVo readCouponInfo(Integer id) {
        GetCouponInfoVO coupon = couponService.getCouponById(id);
        return BaseRespVo.ok(coupon, "成功");

    }


    @RequiresPermissions(value ={"*","admin:coupon:read"},logical = Logical.OR)
    @RequestMapping("listuser")
    public BaseRespVo getCouponUserList(BaseParam param, Integer couponId, Integer userId, Short status) {

        ShowCouponUserListVO list = couponService.getCouponUserList(param, couponId, userId, status);

        return BaseRespVo.ok(list, "成功");

    }

    @RequiresPermissions(value ={"*","admin:coupon:update"},logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo updateCoupon(@RequestBody UpdateCouponBO couponBO) {

        //根据时间类型判断输入时间是否合规
        if (couponBO.getTimeType() == 1) {
            String startTime = couponBO.getStartTime();
            String endTime = couponBO.getEndTime();
            if (startTime == null && endTime != null) {
                return BaseRespVo.fail("请输入起始日期！");
            } else if (endTime == null) {
                return BaseRespVo.fail("请输入截止日期！");
            } else {
                //截止日期小于起始日期
                if (DateUtils.parseDate(startTime).getTime() > DateUtils.parseDate(endTime).getTime()) {
                    return BaseRespVo.fail("起始日期必须小于截止日期");
                }
            }
        } else if (couponBO.getTimeType() == 0) {
            if (couponBO.getDays() <= 0) {
                return BaseRespVo.fail("天数格式不对");
            }
        }
        UpdateCouponVO couponVO = couponService.updateCoupon(couponBO);
        return BaseRespVo.ok(couponVO, "成功");

    }

    @RequiresPermissions(value ={"*"},logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo deleteCoupon(@RequestBody DeleteCouponVO couponVO) {
        Integer integer = couponService.deleteCouponNotReal(couponVO);

        if (integer < 0) {
            return BaseRespVo.fail("规定用户拥有未使用的优惠卷不能删除");
        }
        return BaseRespVo.ok();

    }
    @RequiresPermissions(value ={"*","admin:coupon:create"},logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo createCoupon(@RequestBody CreateCouponBO couponBO) {
        //根据时间类型判断输入时间是否合规
        if (couponBO.getTimeType() == 1) {
            String startTime = couponBO.getStartTime();
            String endTime = couponBO.getEndTime();
            if (startTime == null && endTime != null) {
                return BaseRespVo.fail("请输入起始日期！");
            } else if (endTime == null) {
                return BaseRespVo.fail("请输入截止日期！");
            } else {
                //截止日期小于起始日期
                if (DateUtils.parseDate(startTime).getTime() > DateUtils.parseDate(endTime).getTime()) {
                    return BaseRespVo.fail("起始日期必须小于截止日期");
                }
            }
        } else if (couponBO.getTimeType() == 0) {
            if (couponBO.getDays() <= 0) {
                return BaseRespVo.fail("天数格式不对");
            }
        }
        CreateCouponVO coupon = couponService.createCoupon(couponBO);

        return BaseRespVo.ok(coupon);

    }
}
